﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using NetFtp.Core.Login;
using NetFtp.Core.Ftp;

public partial class UserAccount : System.Web.UI.Page
{
    User user;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["USER_LOGIN"] == null)
            Response.Redirect("Default.aspx");

        UserList liste = new UserList();
        user = liste.ReturnUser(Session["USER_LOGIN"].ToString());
        user.FillAccount();

        if (Page.IsPostBack == false)
        {
            gvAccount.DataSource = user.FtpAccounts;
            gvAccount.DataBind();
        }
    }

    protected void gvAccount_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Supprimer")
        {
            FtpAccount account = user.ReturnAccount(int.Parse(e.CommandArgument.ToString()));
            user.RemoveFtpAccount(account);

            user.FillAccount();
            gvAccount.DataSource = user.FtpAccounts;
            gvAccount.DataBind();
        }
        if (e.CommandName == "Connection")
        {
            FtpAccount account = user.ReturnAccount(int.Parse(e.CommandArgument.ToString()));
            FtpSession ftpSession = new FtpSession(account.Hostname, account.Port, account.Username, account.Password);

            Session["HOST_FTP"] = account.Hostname;
            Session["LOGIN_FTP"] = account.Username;
            Session["MDP_FTP"] = account.Password;
            Session["PORT_FTP"] = account.Port;
            Session["FOLDER_FTP"] = account.Folder;
            Session["PASSIVE_FTP"] = ftpSession.Passive;

            Response.Redirect("Ftp.aspx");
        }
    }
    protected void cmdNew_Click(object sender, EventArgs e)
    {
        Response.Redirect("NewAccount.aspx");
    }
}
